package com.xindong.zhitgx.plugins;

import static com.xindong.zhitgx.MainActivity.mainContext;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

import androidx.annotation.NonNull;

import com.meihu.beauty.utils.MhDataManager;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.listener.OnGetOaidListener;
import com.xindong.zhitgx.MainActivity;
import com.xindong.zhitgx.factory.XinDongBeautyTRTC;

import io.flutter.Log;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class XinDongFlutterPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {
    // 通道
    private MethodChannel methodChannel;
    private final String channelName = "com.xindong.zhitgx";

    private static final String initLink = "initLink"; //
    private static final String initBeauty = "initBeauty"; //
    private static final String saveBeauty = "saveBeauty"; //
    static final String getOaid = "getOaid";
    static final String globalLocationEnable = "globalLocationEnable";
    static final String openGPSSettings = "daKaiGPS";
    private static final String channelInstallData = "channelData"; //

    public void getOaid(MethodChannel.Result result) {
        UMConfigure.getOaid(mainContext, new OnGetOaidListener() {
            @Override
            public void onGetOaid(String oaid) {
                result.success(oaid);
            }
        });
    }

    private void getLocationEnable(MethodChannel.Result result) {
        LocationManager locationManager = (LocationManager) mainContext.getSystemService(Context.LOCATION_SERVICE);
        boolean isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isOpen) {
            result.success(true);
        } else {
            result.success(false);
        }
    }

    private void openGPSSetting() {
        //跳转到手机原生设置页面
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        mainContext.startActivityForResult(intent, 1);
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        // 声明通道，第二个参数 name 要和 Flutter 中定义的通道名称保持一致
        methodChannel = new MethodChannel(binding.getBinaryMessenger(), channelName);
        // 在此通道上注册方法调用将要处理的功能(函数)
        methodChannel.setMethodCallHandler(this);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        Log.e("接收Flutter----", call.method + "；参数：" + call.arguments);
        switch (call.method) {
            case channelInstallData:
                String channel = MainActivity.getDefaultChannel(mainContext);
                result.success(channel);
                break;
            case initLink:
                mainContext.initLink();
                break;
            case initBeauty:
                // 美颜初始化
                MhDataManager.getInstance().create(mainContext);
//                MhDataManager.getInstance().restoreMeiYanData();
                new XinDongBeautyTRTC().setLocalVideoProcessListener(mainContext);
                break;
            case saveBeauty:
                // 美颜保存
                MhDataManager.getInstance().saveMeiYanData();
                break;
            case getOaid:
                getOaid(result);
                break;
            case globalLocationEnable:
                getLocationEnable(result);
                break;
            case openGPSSettings:
                openGPSSetting();
                break;
            default:
                result.notImplemented();
                break;
        }
    }

}
